package com.opengroupe.service.admin.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import com.opengroupe.dao.CandidatDao;
import com.opengroupe.dao.QuestionDao;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.Categorie;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.admin.AdministrationService;
import com.opengroupe.util.QrhException;
import com.opengroupe.util.QrhUtil;
import com.opengroupe.util.ResultatQuestionnaire;

public class AdministrationServiceImpl implements AdministrationService {
	@Autowired
	private CandidatDao candidatDao;
	
	@Autowired
	private QuestionDao questionDao;
	
	private static final Log logger = LogFactory.getLog(AdministrationServiceImpl.class);

	public Question creerQuestion(Question question) {
		if(question == null) {
			return null;
		}

		Question createdQuestion = questionDao.persist(question);
		return createdQuestion;
	}
	
	public Candidat creerCandidat(String nomCandidat, String prenomCandidat,
			String telephoneCandidat, String emailCandidat) throws DataAccessException {

		Candidat candidat = new Candidat(nomCandidat, prenomCandidat,
				emailCandidat, telephoneCandidat, null);
		
		try{
			return candidatDao.persist(candidat);
		} catch (DataAccessException e) {
			logger.error("Erreur lors de la création du candidat");
			throw e;
		}
	}

	public List<CandidatReponseQuestion> affecterQuestionnaire(Candidat candidat, Categorie categorie) throws DataAccessException {
		logger.debug("affectation du questionnaire "+categorie.getLibelleCategorie()+" au candidat"+candidat.getNomCandidat());
		
		List<CandidatReponseQuestion> questionnaire = new ArrayList<CandidatReponseQuestion>(); 
		Set<Question> questions;
		
		try{
			questions = categorie.getQuestions();
		} catch (DataAccessException e) {
			throw e;
		}
		
		for(Question question : questions){
			CandidatReponseQuestion candidatReponseQuestion = new CandidatReponseQuestion();
			candidatReponseQuestion.setCandidat(candidat);
			candidatReponseQuestion.setQuestion(question);
			
			questionnaire.add(candidatReponseQuestion);
		}
		
		return questionnaire;
	}
	

	public ResultatQuestionnaire consulterResultat(Candidat candidat) {
		if(candidat == null) {
			return null;
		}

		Set<CandidatReponseQuestion> crq = candidat.getCandidatreponsequestions();
		return corrigerQuestions(crq);
	}

	public ResultatQuestionnaire corrigerQuestions(Set<CandidatReponseQuestion> questionnaire) {
		Map<Question, Set<Reponse>> questionReponses = new HashMap<Question, Set<Reponse>>();
		Candidat candidat = null;
		for(CandidatReponseQuestion crq: questionnaire) {
			if(candidat == null) {
				candidat = crq.getCandidat();
			}
			
			Question question = crq.getQuestion();
			Reponse reponse = crq.getReponse();
			QrhUtil.updateMap(questionReponses, question, reponse);
		}

		Map<Question, Boolean> resultat = QrhUtil.corrigerQuestionnaire(questionReponses);
		return new ResultatQuestionnaire(candidat, questionReponses, resultat);
	}

	public Question modifierQuestion(Question question) throws QrhException {
		if(question == null) {
			return null;
		}
		
		if(question.getIdQuestion() == null) {
			String message = "L'identifiant de la question a modifie ne peut être nul !";
			throw new QrhException(message);
		}
		
		Question updatedQuestion = questionDao.merge(question);
		return updatedQuestion;
	}

	public boolean identification(String codeAcces) {
		// TODO Auto-generated method stub
		return false;
	}


}
